package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.mysql.cj.conf.ConnectionUrlParser.Pair;

import Dao.RemarkDao;
import Dao.RemarkDaoimpl;
import entity.CommentVO;
import entity.Remark;
import entity.Zremark;
import util.DBManager;
import util.Dbconnect;


public class RemarkDaoimpl implements RemarkDao{
	 /**
		* Title: getall
		* Description: 查看所有总评论
		* @author 邢磊
		*/

		public List<Pair<CommentVO,List<Zremark>>>  getall(){
			List<Pair<CommentVO,List<Zremark>>> lst = new ArrayList<Pair<CommentVO,List<Zremark>>>();
			List<Zremark> ls1= new ArrayList<Zremark>();
	        Connection conn=null;
	        PreparedStatement statement=null;
	        try {
	            conn=DBManager.getConnection();
	            String sql="select CommentID,wordname,email,collocation,example,evaluate,thumbs,unthumbs,z_id,preid,email,contant from commment,Zremark where Zremark.wordname=Comment.wordname";
	            statement=conn.prepareStatement(sql);
	            ResultSet set=statement.executeQuery();
	            while(set.next()) {
	            	CommentVO co=new CommentVO(set.getInt("CommentID"), set.getString("wordname"),set.getString("email"),
	                        set.getString("collocation"),set.getString("example"),set.getString("evaluate "),set.getInt("thumbs"),set.getInt("unthumbs"));
	            	Zremark zk=zk=new Zremark(set.getInt("z_id"),set.getInt("preid"),set.getString("email"),set.getString("contant"),set.getString("wordname"));
	            	ls1.add(zk);
	            	Pair<CommentVO,List<Zremark>> p1 = new Pair<CommentVO, List<Zremark>>(co,ls1);
	            	lst.add(p1);
	            }
	            
	            

	            DBManager.close(statement, conn);
	        } catch (Exception e) {
	            e.printStackTrace();

	        }finally {

	            DBManager.close(statement, conn);
	        }
	        return lst;
		}
	 /**
		* Title: getremark
		* Description: 获得指定单词的总评论
		* @author 邢磊
	 */

		@Override
		public List<Pair<CommentVO,List<Zremark>>>  getremark(String wordname){
			List<Pair<CommentVO,List<Zremark>>> lst = new ArrayList<Pair<CommentVO,List<Zremark>>>();


	        try {	
	        String sql="select * from comment where wordname = ?";
	        PreparedStatement st = Dbconnect.init(sql);        
				st.setString(1,wordname);		
				List<CommentVO> query = Dbconnect.query(st,CommentVO.class);
	        	for(CommentVO c:query) {
	        		
	        		System.out.println(c.getCommentid());
	        		sql="select * from z_remark where preid = ?";
	          		st = Dbconnect.init(sql);
	            	st.setInt(1,c.getCommentid());
	            	List<Zremark> query2 = Dbconnect.query(st,Zremark.class);
	            	Pair<CommentVO,List<Zremark>> p1 = new Pair<CommentVO, List<Zremark>>(c,query2);
	            	lst.add(p1);  
	        	}
	        } catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        return lst;
		}


		@Override
		public void addlike(int CommentID) throws Exception {
			// TODO Auto-generated method stub
			String sql = "update Comment set thumbs=thumbs+1 where CommentID=? ";
			PreparedStatement st = Dbconnect.init(sql);
			st.setInt(1,CommentID);
			Dbconnect.update(st);
			Dbconnect.close(st);

			
		}

		public void addhate(int CommentID) throws Exception {
			// TODO Auto-generated method stub
			String sql = "update Comment set unthumbs=unthumbs+1 where CommentID=?";
			PreparedStatement st = Dbconnect.init(sql);
			st.setInt(1,CommentID);
			Dbconnect.update(st);
			Dbconnect.close(st);
		}
	}